package com.shop.blockquenue;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  一个初始为0的变量，两个线程对其交替操作，一个加一个减，
 *  1、线程    操作      资源类
 *
 */
public class ShareData {

    private static int number = 0;

    private static Lock lock = new ReentrantLock();

    private static Condition condition = lock.newCondition();

    public static void increment() throws InterruptedException {
        lock.lock();
        try {
            while (number != 0){
                // 等待 不能生产
                condition.await();
            }
            number ++ ;
            System.out.println(Thread.currentThread().getName()+"\t "+ number);

            //通知唤醒
            condition.signalAll();
        } finally {
            lock.unlock();
        }

    }

    public static void main(String[] args) {

    }
}
